var prefix = jsctx + "/tenantCarNumber";

var tenantCarNumber = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageCarNum",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            carNo: $('#carNo').val(),
                            carNumberType: $('#carNumberType').val(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'numberId', title: 'ID'},
                        {field: 'carType', title: '车辆型号'},
                        {field: 'brandName', title: '品牌'},
                        {field: 'carNo', title: '车牌号'},
                        {field: 'enterpriseName', title: '所属企业'},
                        {field: '', title: '所属区域', width: '250', align: 'center',
                            formatter: function (value, row, index) {
                            if(row.provinceName==null&&row.cityNam==null&&row.areaName==null){
                                return '-';
                            }else{
                                return '<span>'+row.provinceName+'省'+row.cityName+'市'+row.areaName+'</span>';
                            }

                            }},
                        {field: 'carNumberType', title: '车辆类型',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-primary">小车</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-danger">中巴</span>'
                                }else if (value == '2') {
                                    return '<span class="label label-danger">货车</span>'
                                }

                            }
                        },
                        {field: 'createdTime', title: '注册时间'},
                        {
                            title: '操作',
                            field: 'numberId',
                            width:'400',align: 'center',
                            formatter: function (value, row, index) {
                                var e = '';
                                var d = '';
                                    e = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="tenantCarNumber.edit(\''
                                        + row.numberId
                                        + '\')"><i class="fa fa-edit">编辑</i></a> ';
                                    d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="删除" onclick="tenantCarNumber.deleteCar(\''
                                        + row.numberId
                                        + '\')"><i class="fa fa-edit">删除</i></a> ';

                                return e + d ;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addTenantCarNumber() {
        location.href = prefix + "/addTenantCarNumber";
    }

    function edit(numberId) {
        location.href = prefix + "/editTenantCarNumber/" + numberId;
    }



    function deleteCar(numberId) {
        layer.confirm('确定要删除车辆？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteTenantCarNumber",
                type: "post",
                data: {
                    'numberId': numberId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }



    function goBack() {
        location.href = prefix + "/index";
    }
    /**
     * 导出桩群发票列表
     *
     */
    function exportCarNumber() {
        $.fileDownload(prefix + "/exportCarNumber", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                carNo: $('#carNo').val(),
                carNumberType: $('#carNumberType').val(),

                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...",{time:-1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！",{time:1500});
            },
            failCallback: function (data) {
                var str = "{"+data.substring(data.indexOf("{")+1,data.indexOf("}"))+"}";
                layer.msg(JSON.parse(str).message);
            }
        })
    };

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addTenantCarNumber: addTenantCarNumber,
        edit: edit,
        deleteCar: deleteCar,
        exportCarNumber: exportCarNumber,
    };
})();

var addTenantCarNumber = (function () {
    function init() {
        validateRule();
    };


    function save() {

        $.ajax({
            cache: true,
            type: "POST",
            url:prefix + "/saveOrUpdateCarNum",
            data: $('#frmTenantCarNumer').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    tenantCarNumber.goBack();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantCarNumer").validate({
            rules: {
                enterpriseMemberId: {
                    required: true,
                },
                brandId: {
                    required: true,
                },
                carType: {
                    required: true,
                },
                brandId: {
                    required: true,
                },
                carNo: {
                    required: true,
                }
            },
            messages: {
                enterpriseMemberId: {
                    required: icon + "请选择所属企业"
                },
                brandId: {
                    required: icon + "请选择车辆品牌"
                },
                carType: {
                    required: icon + "请输入车辆型号"
                },
                carNo: {
                    required: icon + "请输入车牌号"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    function searchMember() {
        var mobile = $('#mobile').val();
        $.ajax({
            cache: true,
            type: "GET",
            url:jsctx + "/member/selectMemberByMobile",
            data: {
                mobile:mobile
            },
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    if (data.data != null){
                        $('#memberId').val(data.data.memberId)
                        $('#nickname2').val(data.data.nickname2)
                    }else {
                        parent.layer.msg("找不到用户");
                    }

                } else {
                    parent.layer.msg(data.message);
                }

            }
        });

    }

    function getArea() {
        var cityId = $("#cityId").val().trim();
        if (cityId == null || cityId == '') {
            $("#area").html("");
            var str = "<option value=''>行政区域</option>";
            $("#area").append(str);
        } else {
            $.ajax({
                url: prefix + "/getArea",
                type: "GET",
                data: {
                    cityId: cityId
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    $("#areaId").html("");
                    var str = "<option value=''>--请选择--</option>";
                    if (data.status == 200) {
                        $.each(data.data, function (i, item) {
                            str += "<option value=" + item.areaId + ">" + item.areaName + "</option>";
                        })
                        $("#areaId").append(str);
                    } else {
                        // parent.layer.msg(data.message);
                        $("#areaId").val("");
                    }
                }
            });
        }

    }
    function getCity() {
        var provinceId = $("#provinceId").val().trim();
        if (provinceId == null || provinceId == '') {
            $("#city").html("");
            var str = "<option>市区</option>";
            $("#city").append(str);
        } else {
            $.ajax({
                url: prefix + "/getCity",
                type: "GET",
                data: {
                    provinceId: provinceId
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    $("#cityId").html("");
                    var str = "<option value=''>--请选择--</option>";
                    if (data.status == 200) {
                        $.each(data.data, function (i, item) {
                            str += "<option value=" + item.cityId + ">" + item.cityName + "</option>";
                        })
                        $("#cityId").append(str);
                    } else {
                        // parent.layer.msg(data.message);
                        $("#cityId").val("");
                    }
                }
            });
        }

    }


    return {
        init: init,
        searchMember:searchMember,
        getArea: getArea,
        getCity: getCity
    };
})();

var editTenantCarNumber = (function () {
    function init() {
        validateRule();
    };

    function update() {
        $.ajax({
            cache: true,
            type: "POST",
            url:prefix + "/saveOrUpdateCarNum",
            data: $('#frmTenantCarNumer').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    tenantCarNumber.goBack();
                } else {
                    parent.layer.msg(data.message);
                }

            }
        });
    }


    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantCarNumer").validate({
            rules: {
                enterpriseMemberId: {
                    required: true,
                },
                brandId: {
                    required: true,
                },
                carType: {
                    required: true,
                },
                brandId: {
                    required: true,
                },
                carNo: {
                    required: true,
                }
            },
            messages: {
                enterpriseMemberId: {
                    required: icon + "请选择所属企业"
                },
                brandId: {
                    required: icon + "请选择车辆品牌"
                },
                carType: {
                    required: icon + "请输入车辆型号"
                },
                carNo: {
                    required: icon + "请输入车牌号"
                }
            },
            submitHandler: function () {
                update();
            }
        });
    }

    function getArea() {
        var cityId = $("#cityId").val().trim();
        if (cityId == null || cityId == '') {
            $("#area").html("");
            var str = "<option value=''>行政区域</option>";
            $("#area").append(str);
        } else {
            $.ajax({
                url: prefix + "/getArea",
                type: "GET",
                data: {
                    cityId: cityId
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    $("#areaId").html("");
                    var str = "<option value=''>--请选择--</option>";
                    if (data.status == 200) {
                        $.each(data.data, function (i, item) {
                            str += "<option value=" + item.areaId + ">" + item.areaName + "</option>";
                        })
                        $("#areaId").append(str);
                    } else {
                        // parent.layer.msg(data.message);
                        $("#areaId").val("");
                    }
                }
            });
        }

    }
    function getCity() {
        var provinceId = $("#provinceId").val().trim();
        if (provinceId == null || provinceId == '') {
            $("#city").html("");
            var str = "<option>市区</option>";
            $("#city").append(str);
        } else {
            $.ajax({
                url: prefix + "/getCity",
                type: "GET",
                data: {
                    provinceId: provinceId
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    $("#cityId").html("");
                    var str = "<option value=''>--请选择--</option>";
                    if (data.status == 200) {
                        $.each(data.data, function (i, item) {
                            str += "<option value=" + item.cityId + ">" + item.cityName + "</option>";
                        })
                        $("#cityId").append(str);
                    } else {
                        // parent.layer.msg(data.message);
                        $("#cityId").val("");
                    }
                }
            });
        }

    }

    return {
        init: init,
        getArea: getArea,
        getCity: getCity
    };
})();

